﻿using System;

using System.Collections.Generic;
using System.Text;
using ScannerApp.Utils;
using System.Collections;

namespace ScannerApp.ScannerWebserviceWrapper
{

    public class OutStockOrderResult : BaseOrderResult
    {
        public OutStockHeader despatchListHeadBean;

        public int payComplete;

        public List<OutStockItem> despatchListBodyBeans;

        public List<ScannedOutStockGoods> tempDespatchBodyList;

        public List<Product> products;

        public int orderTotalPage;
        public int orderTotalCount;
        private int orderPageIndex;
        private int scanTotalPage;
        private int scanTotalCount;
        private int scanPageIndex;

        public int OrderPageIndex
        {
            get { return orderPageIndex; }
            set { orderPageIndex = value; }
        }

        public int ScanPageIndex
        {
            get { return scanPageIndex; }
            set { scanPageIndex = value; }
        }

        public int ScanTotalPage
        {
            get { return scanTotalPage; }
            set { scanTotalPage = value; }
        }

        public int ScanTotalCount
        {
            get { return scanTotalCount; }
            set { scanTotalCount = value; }
        }

        public int OrderTotalPage
        {
            get { return orderTotalPage; }
            set { orderTotalPage = value; }
        }

        public int OrderTotalCount
        {
            get { return orderTotalCount; }
            set { orderTotalCount = value; }
        }

        public bool IsFinishedStatus()
        {
            bool result = (this.despatchListHeadBean.id == 3) ? true : false;
            return result;
        }

        #region IScanProduct 成员
        public override bool IsProductCodeInOrder(string productCode)
        {
            foreach (OutStockItem rec in this.despatchListBodyBeans)
            {
                if (rec.productCode.Equals(productCode))
                {
                    return true;
                }
            }
            return false;
        }


        public override bool IsProductEnablingQR(string productCode)
        {
            if (this.products != null)
            {
                foreach (Product product in this.products)
                {
                    if (product.code.Equals(productCode))
                    {
                        if (product.xlcode == true)
                        {
                            return true;
                        }
                    }
                }
            }

            return false;
        }

        public override  String GetHeaderInfo()
        {
            //return String.Format("捡货单号:{0}", despatchListHeadBean.pickingId);
            return string.Empty;
        }

        public override bool IsQRInOrder(string barcode)
        {
            //only scan QR for out stock order.
            return true;
        }

        public override bool IsScanShelfAfterQuantity()
        {
            return true;
        }

        #endregion

        public bool IsRepeatedScan(string barcode)
        {
            if (tempDespatchBodyList != null)
            {
                foreach (ScannedOutStockGoods rec in this.tempDespatchBodyList)
                {
                    if (barcode.Equals(rec.qrCode) && (rec.currentAmt != 0))
                    {
                        return true;
                    }
                }

            }
            return false;
        }

        private Product GetProduct(String productCode)
        {
            if (this.products != null)
            {
                foreach (Product p in this.products)
                {
                    if (productCode.Equals(p.code))
                    {
                        return p;
                    }
                }
            }
            return null;
        }

        public bool HasOrderFinished()
        {
            bool returnValue = false;

            if (this.GetMergedOutStockProducts().Count == 0)
            {
                returnValue = true;
            }

            return returnValue;

            /*
            //先检查所有指定qr的数据。

            Hashtable scannedQrHashtable = new Hashtable();
            foreach (OutStockItem item in this.despatchListBodyBeans)
            {
                if (!item.qrCode.Equals(""))
                {
                    bool foundQr = false;
                    foreach (ScannedOutStockGoods temp in this.tempDespatchBodyList)
                    {
                        if (item.qrCode.Equals(temp.qrCode))
                        {
                            foundQr = true;
                            scannedQrHashtable.Add(item.qrCode, temp);
                            break;
                        }
                    }
                    if (foundQr == false)
                    {
                        return false;
                    }
                    
                }
            }

            //检查未指定qr的数据
            foreach (OutStockItem item in this.despatchListBodyBeans)
            {
                if (!item.qrCode.Equals(""))
                {
                    continue;
                }

                float scannedQuantity = 0;
                foreach (ScannedOutStockGoods temp in this.tempDespatchBodyList)
                {

                    if (temp.qrCode != null && scannedQrHashtable.ContainsKey(temp.qrCode))
                    {
                        continue;
                    }
                    Product p = GetProduct(temp.productCode);
                    if (p != null)
                    {
                        //if qr is enabling, caculate the quantity according to product code and batch number alone. 
                        if (p.xlcode == true)
                        {
                            if (temp.productCode.Equals(item.productCode) && temp.batch.Equals(item.batch))
                            {
                                scannedQuantity += temp.currentAmt;
                            }
                        }
                        else  //check the position name. 
                        {
                            if (temp.productCode.Equals(item.productCode) && temp.batch.Equals(item.batch) && temp.positionCode.Equals(item.positionCode))
                            {
                                scannedQuantity += temp.currentAmt;
                            }
                        }

                    }
                }

                if (scannedQuantity != Convert.ToInt32(item.amount))
                {
                    return false;
                }

            }         
            return true;
            */
        }

        public IList<MergedOutStockProduct> GetMergedOutStockProducts()
        {
            Hashtable inventorys = new Hashtable();
            Hashtable directQRs = new Hashtable();
            IList<MergedOutStockProduct> result = new List<MergedOutStockProduct>();
            foreach (OutStockItem rec in this.despatchListBodyBeans)
            {

                String key = rec.productCode + "|" + rec.batch + "|" + rec.positionCode;
                if (rec.qrCode!= null && rec.qrCode.Equals("") == false)
                {
                    key = rec.qrCode;
                    directQRs.Add(key, rec);
                }

                MergedOutStockProduct mergedRec = new MergedOutStockProduct();
                mergedRec.serialNumber = rec.qrCode;
                mergedRec.inventoryCode = rec.productCode;
                mergedRec.batchNumber = rec.batch;
                mergedRec.positionCode = rec.positionCode;
                mergedRec.totalCount = rec.amount;

                inventorys.Add(key, mergedRec);
                result.Add(mergedRec);
            }

            /*
            //去除已扫描产品
            if (this.tempDespatchBodyList != null)
            {
                foreach (ScannedOutStockGoods rec in this.tempDespatchBodyList)
                {
                    String key = rec.productCode + "|" + rec.batch + "|" + rec.positionCode;
                    if (rec.qrCode != null && directQRs.ContainsKey(rec.qrCode))
                    {
                        key = rec.qrCode;
                    }
                    MergedOutStockProduct mergedRec = inventorys[key] as MergedOutStockProduct;
                    if (mergedRec != null)
                    {
                        mergedRec.totalCount -= rec.currentAmt;

                        if (mergedRec.totalCount == 0)
                        {
                            result.Remove(mergedRec);
                        }
                    }
                }
            }
            */
            return result;
        }
    }

    

    public class OutStockHeader
    {

        public int id;

	    public string despatchNo; //货运清单号
    	
	    public int salesOrderHeadId; 

        public int pickingListHeadId;

        public int salesTypeId; 
    	
	    public int businessTypeId; 
    	
	    public int warehouseId; 

        public int status; 
    	
	    public int despatchTypeId; 
    	
	    public int distributorId; 
    	
        public bool isDeleted;

        public string expressNo;


    }

    public class OutStockItem
    {

        public int id;

        public int despatchListHeadId;

        public int qrExtantAmountId;

        public int batchExtantAmountId;

	    public  String despatchNo ; //发运清单号

        [DataTableFieldAttribute(HeaderText = "序列号")]
	    public  String qrCode; //qrCode

        [DataTableFieldAttribute(HeaderText = "产品代码")]
	    public  String productCode; //产品代码
    	
	    public  String productName; //产品名称

        [DataTableFieldAttribute(HeaderText = "批次")]
        public String batch; //批次


        [DataTableFieldAttribute(HeaderText = "数量")]
        public float amount; //数量
    	
	    public  String warehouseName; //仓库名称

        [DataTableFieldAttribute(HeaderText = "货位号")]
        public String positionCode; //货位名称

    }

    public class ScannedOutStockGoods
    {
        public int id;

        public String despatchListHeadId; //发运清单号

        [DataTableFieldAttribute(HeaderText = "序列号")]
        public String qrCode;

        [DataTableFieldAttribute(HeaderText = "产品代码")]
        public String productCode; //产品代码

        [DataTableFieldAttribute(HeaderText = "批次")]
        public String batch; //批次

        [DataTableFieldAttribute(HeaderText = "数量")]
        public float currentAmt; //当前数量

        [DataTableFieldAttribute(HeaderText = "货位号")]
        public String positionCode; //货位名称


    }

    public class MergedOutStockProduct
    {

        [DataTableFieldAttribute(HeaderText = "序列号")]
        public String serialNumber;

        [DataTableFieldAttribute(HeaderText = "产品代码")]
        public String inventoryCode = string.Empty;//产品代码


        [DataTableFieldAttribute(HeaderText = "批次")]
        public String batchNumber = string.Empty;//批号

        [DataTableFieldAttribute(HeaderText = "未扫描数量")]
        public float totalCount = 0;

        [DataTableFieldAttribute(HeaderText = "货位号")]
        public String positionCode; //货位名称

    }
    
}
